﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.orderLinqDAL;
using dhSystemBLL.orderData;
using dhSystemBLL.basicData;


namespace dhsystem.orderForm
{
    public partial class FrmCustListAdd : Form
    {
        custListBLL cust = new custListBLL();
        custTypeBLL custType = new custTypeBLL();
        custListModel custListRow = new custListModel();
        workerListBLL workerList = new workerListBLL();
        FrmCustList custList = new FrmCustList();
        BindingSource BindingSourcecustListAdd;
        string state;
        string workerNum;
        //绑定数据源
        public FrmCustListAdd(BindingSource bindingSourceCustList, string _state, string _workerNum)
        {
            InitializeComponent();
            state = _state;
            workerNum = _workerNum;
            BindingSourcecustListAdd = bindingSourceCustList;

        }
        //绑定数据源
        public FrmCustListAdd(BindingSource bindingSourceCustList, string _state)
        {
            InitializeComponent();
            state = _state;
            BindingSourcecustListAdd = bindingSourceCustList;

        }
        //打开窗口时绑定数据
        private void FrmCustListAdd_Load(object sender, EventArgs e)
        {
            textCustNum.Focus();
            bindingSourceworkerList.DataSource = workerList.getworkerListAll();
            comboBoxWorkerNum.DataSource = bindingSourceworkerList;
            comboBoxWorkerNum.DisplayMember = "name";
            comboBoxWorkerNum.ValueMember = "workerNum";
           
            bindingSourcecustType.DataSource = custType.getcustTypeAll();
            comboBoxCType.DataSource = bindingSourcecustType;
            comboBoxCType.DisplayMember = "cType";
            comboBoxCType.ValueMember = "cType";
            if (state == "编辑")
            {
                textCustNum.DataBindings.Add("text", BindingSourcecustListAdd, "custNum");
                textCustName.DataBindings.Add("text", BindingSourcecustListAdd, "custName");
                textCustAdd.DataBindings.Add("text", BindingSourcecustListAdd, "custAdd");
                textPhone.DataBindings.Add("text", BindingSourcecustListAdd, "phone");
                textlinkMan.DataBindings.Add("text", BindingSourcecustListAdd, "linkMan");
                textPostCode.DataBindings.Add("text", BindingSourcecustListAdd, "postCode");
                textQQ.DataBindings.Add("text", BindingSourcecustListAdd, "qq");
                textMobilePhone.DataBindings.Add("text", BindingSourcecustListAdd, "mobilePhone");
                textFax.DataBindings.Add("text", BindingSourcecustListAdd, "fax");
                comboBoxWorkerNum.DataBindings.Add("text", BindingSourcecustListAdd, "workerNum");
                textPayType.DataBindings.Add("text", BindingSourcecustListAdd, "payType");
                comboBoxtaxMark.DataBindings.Add("text", BindingSourcecustListAdd, "taxMark");
                comboBoxCType.DataBindings.Add("text", BindingSourcecustListAdd, "cType");
                textCtypeMark.DataBindings.Add("text", BindingSourcecustListAdd, "ctypeMark");
                textCTypeParent.DataBindings.Add("text", BindingSourcecustListAdd, "cTypeParent");
                textCreateUser.DataBindings.Add("text", BindingSourcecustListAdd, "createUser");
                textUpdateUser.DataBindings.Add("text", BindingSourcecustListAdd, "updateUser");
                textmark.DataBindings.Add("text", BindingSourcecustListAdd, "mark");
                comboBoxWorkerNum.SelectedValue = workerNum;
            }
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (state == "新增")
            {
                if (textCustName.Text =="")
                {
                    MessageBox.Show("客户名不能为空");
                }
                else
                {
                    try
                    {
                        DialogResult result = MessageBox.Show("是否新增客户资料！", "提示信息", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            custListRow.custNum = textCustNum.Text;
                            custListRow.custName = textCustName.Text;
                            custListRow.custAdd = textCustAdd.Text;
                            custListRow.phone = textPhone.Text;
                            custListRow.linkMan = textlinkMan.Text;
                            custListRow.postCode = textPostCode.Text;
                            custListRow.qq = textQQ.Text;
                            custListRow.mobilePhone = textMobilePhone.Text;
                            custListRow.fax = textFax.Text;
                            custListRow.workerNum = comboBoxWorkerNum.SelectedValue.ToString();
                            custListRow.payType = textPayType.Text;
                            custListRow.taxMark = comboBoxtaxMark.Text;
                            custListRow.cType = comboBoxCType.Text;
                            custListRow.ctypeMark = textCtypeMark.Text;
                            custListRow.ctypeparent = textCTypeParent.Text;
                            custListRow.createDate = DateTime.Now;
                            custListRow.createUser = textCreateUser.Text;
                            custListRow.updateDate = DateTime.Now;
                            custListRow.updateUser = textUpdateUser.Text;
                            custListRow.mark = textmark.Text;
                            cust.custListInsert(custListRow);
                            bindingSourcecustList.DataSource = cust.getcustListAll();
                            MessageBox.Show("新增成功");
                        }
                    }

                    catch (FormatException ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
            }
            if (state == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否修改客户资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        custListRow.custNum = textCustNum.Text;
                        custListRow.custName = textCustName.Text;
                        custListRow.custAdd = textCustAdd.Text;
                        custListRow.phone = textPhone.Text;
                        custListRow.linkMan = textlinkMan.Text;
                        custListRow.postCode = textPostCode.Text;
                        custListRow.qq = textQQ.Text;
                        custListRow.mobilePhone = textMobilePhone.Text;
                        custListRow.fax = textFax.Text;
                        custListRow.workerNum = comboBoxWorkerNum.SelectedValue.ToString();
                        custListRow.payType = textPayType.Text;
                        custListRow.taxMark = comboBoxtaxMark.Text;
                        custListRow.cType = comboBoxCType.Text;
                        custListRow.ctypeMark = textCtypeMark.Text;
                        custListRow.ctypeparent = textCTypeParent.Text;
                        custListRow.createDate = DateTime.Now;
                        custListRow.createUser = textCreateUser.Text;
                        custListRow.updateDate = DateTime.Now;
                        custListRow.updateUser = textUpdateUser.Text;
                        custListRow.mark = textmark.Text;
                        cust.custListUpdate(custListRow);
                        bindingSourcecustList.DataSource = cust.getcustListAll();
                        MessageBox.Show("修改成功");

                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this .Close ();
        }

    }
}
